/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 统一后端提交返回数据
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/12/09
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191209, 添加注释)
 */
const commonReturn = {
  /**列表数据的获取统一方法*/
  getList (url, opt, self) {
    self.$post(url, Object.assign(opt)).then(res => {
      if (res.code === 0) {
        self.loadData = res.data.records
        self.queryParam.totalCount = res.data.total
      } else {
        self.$message.error(res.message)
      }
    }).catch(res => {
      self.$message.error(res.message)
    })
  },

  /**新增修改接口获取统一方法*/
  toAddEdit (url, opt, self) {
    let _this = self
    _this.$post(url, Object.assign(opt)).then(res => {
      if (res.code === 0) {
        _this.visible = false;
        _this.$parent.queryParam.pageNo = 1;
        _this.$parent.init();
        _this.$message.success('保存成功！');
        _this.id = 0;
        _this.cancel();
      } else {
        _this.$message.error(res.message)
      }
    })
  },

  /**删除修改接口获取统一方法*/
  toDelete (url, prams, self ,text ,opt) {
    self.$confirm({
      title: `确定要删除 ${text} ${opt}吗?`,
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        let _this = self;
        _this.$post(url, prams).then(res => {
          if (res.code === 0) {
            if(_this.loadData.length === 1 && _this.queryParam.pageNo>1){
              _this.queryParam.pageNo = _this.queryParam.pageNo-1;
            }
            _this.init();
            _this.$message.success(`删除 ${text} ${opt}成功！`);
          } else {
            _this.$message.error(res.message)
          }
        })
      }
    })
  }

}

export default commonReturn
